initUsers();
function initUsers() {
    $.ajax({
        url: '/role.do',
        method: 'get',
        success: function (result) {
            if (result && result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    $(`<tr><td>${val}</td></tr>`).appendTo('tbody');
                }
            }
        }
    })
}

$('#role').click(function () {
    swal({
        title: '添加角色',
        html: `<input id="username" class="swal2-input">`,
        preConfirm: function () {
            return new Promise(function (resolve) {
                resolve([
                    $('#username').val(),
                ])
            })
        },
        onOpen: function () {
            $('#username').focus()
        }
    }).then(function (result) {
        $.ajax({
            url: '/roles.do',
            method: 'post',
            data: `username= ${$('#username').val()}`,
            success: function (result) {
                console.log(result);
                if (result == 'success') {
                    location.reload();
                }else if(result === 'no') {
                    swal('用户已添加','添加失败','error');
                }else{
                    swal('添加失败', '请完善信息', 'error');
                }
            }
        });

    }).catch(swal.noop)
});